package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.workflow.model.BpmDefAuthorize;
import com.ccp.dev.workflow.service.BpmDefAuthorizeService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分管授权
 * @author zhanghuichao zhhchyouxiang@126.com
 * @date 2019/7/8 16:10
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/bpmDefAuthorize")
@Action(ownermodel=SysAuditModelType.PROCESS_MANAGEMENT)
public class BpmDefAuthorizeController extends SysBaseController {

    @Resource
    private BpmDefAuthorizeService service;

    /**
     * 分管授权列表页面
     * @return 跳转路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "查看分管授权页面",detail = "查看分管授权页面")
    public String viewList() {
        return getAutoView();
    }

    /**
     * 分管授权新增页面
     * @param request request
     * @param model 模型对象
     * @return 路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description = "操作新增或修改分管授权页面",detail = "操作新增或修改分管授权页面")
    public String viewAdd(HttpServletRequest request, Model model){
        String id = RequestUtil.getString(request,"id");
        String type = RequestUtil.getString(request,"type");
        if(StringUtil.isNotEmpty(id)){
            model.addAttribute("id",id);
            model.addAttribute("type",type);
        }
        return getAutoView();
    }

    /**
     * 分管授权 选择授权人员页面
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/userDialog")
    @Action(description = "操作分管授权-选择授权人员页面",detail = "操作分管授权-选择授权人员页面")
    public String viewUserDialog(){
        return getAutoView();
    }

    /**
     * 分管授权 选择流程页面
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/actDialog")
    @Action(description = "操作分管授权-选择授权流程页面",detail = "操作分管授权-选择授权流程页面")
    public String viewActDialog(){
        return getAutoView();
    }

    /**
     * 分管授权详细信息
     * @param id 数据id
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @Action(description = "查询分管授权详细信息",detail = "查询数据id为${id}的分管授权详细信息<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData get(String id){
        try{
            BpmDefAuthorize bpmDefAuthorize = service.getOneById(id);
            SysAuditThreadLocalHolder.putParamerter("id", id);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("operation.success",bpmDefAuthorize);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 根据权限描述查询分管授权的记录条数
     * 在新增或修改分管授权信息时，权限描述唯一
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/getCountByDesc")
    @Action(description = "根据条件查询分管授权数据条数",detail = "根据条件查询分管授权数据条数<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData getCountByDesc(HttpServletRequest request){
        try{
            QueryFilter queryFilter = new QueryFilter(request, false);
            queryFilter.addFilter("tenantId",ContextUtil.getCurrentUserTenantId());
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("operation.success",service.getCountByDesc(queryFilter));
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 分管授权数据列表数据
     * @param request request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "查询分管授权数据列表数据",detail = "查询分管授权数据列表数据<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData list(HttpServletRequest request){
        if(ContextUtil.isSuperAdmin()){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("superAdmin.is.forbidden");
        }
        try{
            QueryFilter queryFilter = new QueryFilter(request, true);
            //若用户名称查询条件为“所有”，“所有用”，“所用用户”时，将user值变更为“all”可查询所有
            String user = RequestUtil.getString(request,"Q_user_SL");
            if(user.startsWith(StringPool.ALL_CH)){
                queryFilter.replace("user","all");
            }
            queryFilter.addFilter("tenantId",ContextUtil.getCurrentUserTenantId());
            List<BpmDefAuthorize> list = service.queryList(queryFilter);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),list);
        }catch (Exception e){
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 保存或修改分管授权信息
     * @param request request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description = "保存或修改分管授权信息",detail = "<#if id==\"\">保存<#else>修改数据id为${id}的</#if>分管授权信息<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData add(HttpServletRequest request){
        String id = RequestUtil.getString(request,"id","");
        SysAuditThreadLocalHolder.putParamerter("id", id);
        String authorizeDesc = RequestUtil.getString(request,"authorizeDesc");
        String authorizeTypes = RequestUtil.getString(request,"authorizeTypes");
        String authorizeUsers = RequestUtil.getString(request,"authorizeUsers");
        authorizeUsers = StringEscapeUtils.unescapeHtml4(authorizeUsers);
        String authorizeActs = RequestUtil.getString(request,"authorizeActs");
        authorizeActs = StringEscapeUtils.unescapeHtml4(authorizeActs);
        BpmDefAuthorize authorize = new BpmDefAuthorize();
        authorize.setId(id);
        authorize.setTenantId(ContextUtil.getCurrentUserTenantId());
        authorize.setAuthorizeDesc(authorizeDesc);
        authorize.setAuthorizeTypes(authorizeTypes);
        authorize.setOwnerNameJson(authorizeUsers);
        authorize.setDefNameJson(authorizeActs);
        try{
            service.addOrUpdate(authorize);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("save.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("save.failure");
        }
    }

    /**
     * 根据分管授权id删除分管授权记录
     * @param id 分管授权id
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @Action(description = "删除分管授权记录",detail = "删除数据id为${id}的分管授权记录<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData del(String id){
        try{
            service.delAuthorizeById(id);
            SysAuditThreadLocalHolder.putParamerter("id", id);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("delete.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("delete.failure");
        }
    }

    /**
     * 批量删除分管授权记录
     * @param ids 分管授权id数组
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE_BATCH)
    @Action(description = "批量删除分管授权记录",detail = "批量删除分管授权记录，数据id包括：${ids},操作<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData delBatch(@RequestParam(value = "ids[]") String[] ids){
        try{
            service.delAuthorizeByIdArray(ids);
            SysAuditThreadLocalHolder.putParamerter("ids", StringUtils.join(ids, ","));
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("delete.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("delete.failure");
        }
    }

    /**
     * 通过授权的流程反查流程分类
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/getFlowTypeByFlow")
    @Action(description = "通过授权的流程反查流程分类",detail = "通过授权的流程反查流程分类<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData getFlowTypeByFlow(HttpServletRequest request){
        QueryFilter queryFilter = new QueryFilter(request,false);
        try{
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("operation.success",service.getFlowTypeByFlow(queryFilter));
        }catch (Exception e){
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("operation.failure");
        }
    }

}
